// Package supports daemon

package supports

import (
	"os"
	"os/signal"
	"syscall"
)

// Daemon interface
type Daemon interface {
	Init() error
	Start() error
	Stop() error
}

// Run func
func Run(s Daemon) error {
	if err := s.Init(); err != nil {
		return err
	}
	if err := s.Start(); err != nil {
		return err
	}

	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)

	select {
	case <-sigChan:
	}
	return s.Stop()

}
